//图层控制

namespace('n2.layerControl');

n2.layerControl = function() {
	this._viwer = null;
	this._camera = null;
	this._handler = null;
	this._scene = null;
	this._layerS = null;
	n2.base(this, n2.main);
};

n2.definePrototype(n2.layerControl.prototype, {
	viewer: {
		get: function() {
			return this._viewer;
		},
		set: function(value) {
			this._viewer = value;
		}
	},
	handler: {
		get: function() {
			return this._handler;
		},
		set: function(value) {
			this._handler = value;
		}
	},
	camera: {
		get: function() {
			return this._camera;
		},
		set: function(value) {
			this._camera = value;
		}
	},
	scene: {
		get: function() {
			return this._scene;
		},
		set: function(value) {
			this._scene = value;
		}
	},
	layerS: {
		get: function() {
			return this._layerS;
		},
		set: function(value) {
			this._layerS = value;
		}
	}
});

n2.layerControl.prototype.init = function() {
	var self = this;
	var viewer = n2.viewer;
	var layerS = viewer.scene.imageryLayers;
	var collection = []; //图层集合
	//按顺序加载
	//市界，省界，国界，全球国界
	collection = [{
			nameC: '市界',
			nameE: 'cityBoundary',
			layermap: new Cesium.WebMapServiceImageryProvider({
				url: 'http://' + n2.geoServer + '/geoserver/gwc/service/wms?LAYERS=china%3AcityBoundary&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&SRS=EPSG%3A4326&WIDTH=256&HEIGHT=256',
				layers: 'china:cityBoundary',
				parameters: {
					format: 'image/png'
				},
				enablePickFeatures: false //选项框         
			})
		},
		{
			nameC: '省界',
			nameE: 'provinceBoundary',
			layermap: new Cesium.WebMapServiceImageryProvider({
				url: 'http://' + n2.geoServer + '/geoserver/gwc/service/wms?LAYERS=china%3AprovinceBoundary&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&SRS=EPSG%3A4326&WIDTH=256&HEIGHT=256',
				layers: 'china:provinceBoundary',
				parameters: {
					format: 'image/png'
				},
				enablePickFeatures: false //选项框         
			})
		},
		{
			nameC: '国界',
			nameE: 'ChinaBoundary',
			layermap: new Cesium.WebMapServiceImageryProvider({
				url: 'http://' + n2.geoServer + '/geoserver/gwc/service/wms?LAYERS=china%3AChinaBoundary&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.0&REQUEST=GetMap&STYLES=&SRS=EPSG%3A4326&WIDTH=256&HEIGHT=256',
				layers: 'china:ChinaBoundary',
				parameters: {
					format: 'image/png'
				},
				enablePickFeatures: false //选项框         
			})
		},
		{
			nameC: '全球国界',
			nameE: 'worldBoundary',
			layermap: new Cesium.WebMapServiceImageryProvider({
				url: 'http://' + n2.geoServer + '/geoserver/gwc/service/wms?LAYERS=china%3AchinaVector2015&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.0&REQUEST=GetMap&STYLES=&SRS=EPSG%3A4326&WIDTH=256&HEIGHT=256',
				layers: 'china:chinaVector2015',
				parameters: {
					format: 'image/png'
				},
				enablePickFeatures: false //选项框         
			})
		}
	];

	//加载所有图层
	for(var i = 1; i <= collection.length; i++) {
		layerS.addImageryProvider(collection[i - 1].layermap, i);
	}
	
	var toggle = document.querySelectorAll(".toggle");
	//获取所有的图层选择按钮（眼)睛）
	mui('body').on('tap', '.toggle', function(e) {
		var index = null;
		for(var i = 0; i < toggle.length; i++) {
			if(this === toggle[i]) {
				index = i;
			}
		}
		var src = this.src.substr(-5, 1);
		if(src === 'y') {
			this.src = 'images/Artboard2Copy2.png';
			layerS.get(index + 1).alpha = 0;
//			console.log(layerS)
		} else {
			this.src = 'images/Artboard2Copy.png';
			var val = this.parentNode.parentNode.lastElementChild.value || 100;
			layerS.get(index + 1).alpha = val / 100;
		}
	})
	//为相应的划块绑定对应的图层，划块滑动改变对应图层的透明度 ，初始透明度都为1
	var rangeCollection = document.querySelectorAll('.range1');
	for(var i = 0; i < rangeCollection.length; i++) {
		(function(i){
			rangeCollection[i].onchange = function() {
			layerS.get(i+1).alpha = this.value / 100;
			var src = this.parentNode.firstChild.nextSibling.firstChild.src.substr(-5, 1);
			if(src === '2') {
				this.parentNode.firstChild.nextSibling.firstChild.src = 'images/Artboard2Copy.png';
			}
		}
		})(i);
	}

	//点击切换二三维球图标
	var btnTDS = document.querySelector("#btnTDS");
	btnTDS.addEventListener("tap", function() {
		btnTDS.firstElementChild.className != "globeicon" ? btnTDS.firstElementChild.className = "globeicon" : btnTDS.firstElementChild.className = "";
	})
	
}